package com.yuju.controller.order;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.account.enums.SettleAccountReturnCodeEnum;
import com.yuju.common.Result;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.log.service.IRoomLogService;
import com.yuju.model.account.AccountModel;
import com.yuju.order.service.IOrderAccountService;
import com.yuju.vo.account.AccountVO;

/**
 * 类名：OrderAccountController  <br />
 *
 * 功能：
 *
 * @author : jiaokn <br />
 * @Date : 2017年12月13日 下午2:17:07  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("orderAccount")
public class OrderAccountController extends BaseController<AccountVO, AccountModel> {
	
	@Autowired
	private IOrderAccountService orderAccountService;
	/**
	 * 功能：走结<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月6日 下午5:55:09 <br/>
	 */
	@RequestMapping("saveZJ")
	@ResponseBody
	public Result saveZJ(@RequestBody JSONObject obj){
		AccountVO accountVO = obj.toJavaObject(AccountVO.class);
		String empId = accountVO.getEmpId();
		String gowhy = obj.getString("gowhy");
		String roomId = obj.getString("roomId");
		Result result = new Result();
		int rows = orderAccountService.saveSettleAccount(accountVO.getRoomOrderId(), gowhy, empId, roomId);
		if (rows > 0){
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_ZJ_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 功能：退房<br/>
	 *
	 * @author jiaokn
	 * @Date 2017年12月5日 下午2:20:02 <br/>
	 */
	@RequestMapping("endSettle")
	@ResponseBody
	public Result endSettle(@RequestBody JSONObject obj){
		String roomId = obj.getString("roomId");
		String roomOrderId = obj.getString("roomOrderId");
		String empId = obj.getString("empId");
		
		JSONArray jsonArray = obj.getJSONArray("accounts");
		String jsonString = jsonArray.toJSONString();
		ArrayList<AccountVO> accounts = JSON.parseObject(jsonString, new TypeReference<ArrayList<AccountVO>>(){});
		for (AccountVO accountVO : accounts) {
			accountVO.setHotelCode(obj.getString("hotelCode"));
		}
		Result result = new Result();
		int rows = orderAccountService.saveCheckOut(convert(accounts), roomId,roomOrderId,empId);
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TF_SUCCESS.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TF_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TF_FAILURE.getCode());
			result.setReturnMsg(SettleAccountReturnCodeEnum.SETTLEACCOUNT_TF_FAILURE.getDesc());
		}
		return result;
	}
	
}
